from boot import QLineEdit, QIcon, Qt, IMG_ROOT_DIR, Union, QWidget, QAction, QRegExp, QRegExpValidator, LineEditMenu, QPalette, QColor


__all__ = ["QQ9LineEdit", "QQ9NumberLineEdit", "QQ9SearchLineEdit", "QQ9NoSizeLineEdit", "QQ9NoSizeNumberLineEdit"]


class _QQ9LineEditBase(QLineEdit):

    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent=parent)

        # 基础设置
        self.setFixedSize(250, 35)
        self.setContentsMargins(0, 0, 0, 0)
        self.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)

        palette = self.palette()
        palette.setColor(QPalette.Highlight, QColor(0, 153, 255))  # 选择的背景色
        palette.setColor(QPalette.HighlightedText, QColor(255, 255, 255))  # 选择文本的颜色
        self.setPalette(palette)

        # 初始化叉号图标
        self.clear_action: Union[QAction, None] = None

        # 设置样式
        self._set_style_sheet()

    def _set_style_sheet(self):
        self.setStyleSheet(
            """
            QLineEdit {
                background-color: #F5F6F7;
                border: none;
                border-radius: 4px;
                font-size: 12px;
                font-family: "微软雅黑";
                color: #333333;
                padding-left: 5px;  /* 留出左侧图标的空间 */
            }
            QLineEdit:focus {
                border: 1px solid #0099FF;
            }
            """
        )


class _QQ9NoSizeLineEditBase(QLineEdit):

    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent=parent)

        # 基础设置
        self.setContentsMargins(0, 0, 0, 0)
        self.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)

        palette = self.palette()
        palette.setColor(QPalette.Highlight, QColor(0, 153, 255))  # 选择的背景色
        palette.setColor(QPalette.HighlightedText, QColor(255, 255, 255))  # 选择文本的颜色
        self.setPalette(palette)

        # 初始化叉号图标
        self.clear_action: Union[QAction, None] = None

        # 设置样式
        self._set_style_sheet()

    def _set_style_sheet(self):
        self.setStyleSheet(
            """
            QLineEdit {
                background-color: #F5F6F7;
                border: none;
                border-radius: 4px;
                font-size: 12px;
                font-family: "微软雅黑";
                color: #333333;
                padding-left: 5px;  /* 留出左侧图标的空间 */
            }
            QLineEdit:focus {
                border: 1px solid #0099FF;
            }
            """
        )

    def focusInEvent(self, event):
        super().focusInEvent(event)
        if not self.clear_action:
            clear_icon = QIcon(IMG_ROOT_DIR + "/clear.png")
            self.clear_action = self.addAction(clear_icon, QLineEdit.TrailingPosition)
            self.clear_action.triggered.connect(self.clear)

    def focusOutEvent(self, event):
        super().focusOutEvent(event)
        if self.clear_action:
            self.removeAction(self.clear_action)
            self.clear_action = None

    def clear(self):
        super().clear()
        self.clearFocus()

    def contextMenuEvent(self, event):
        menu = LineEditMenu(self)
        menu.exec(event.globalPos())


class QQ9LineEdit(_QQ9LineEditBase):
    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent=parent)


class QQ9NoSizeLineEdit(_QQ9NoSizeLineEditBase):

    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent=parent)

        self.textChanged.connect(self.on_text_changed)

    def on_text_changed(self, text):
        if "MM²" in text:
            self.setText(text.replace("MM²", "mm²"))


class QQ9NumberLineEdit(QQ9LineEdit):

    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent=parent)

        reg_exp = QRegExp(r"\d+")
        validator = QRegExpValidator(reg_exp)
        self.setValidator(validator)

        self.textChanged.connect(self.on_text_changed)

    def text(self):
        value = super().text()
        return value if value != "" else "0"

    def on_text_changed(self, text):
        if len(text) > 1 and text[0] == "0":
            self.setText(text[1:])


class QQ9NoSizeNumberLineEdit(_QQ9NoSizeLineEditBase):

    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent=parent)

        reg_exp = QRegExp(r"\d+")
        validator = QRegExpValidator(reg_exp)
        self.setValidator(validator)

        self.textChanged.connect(self.on_text_changed)

    def text(self):
        value = super().text()
        return value if value != "" else "0"

    def on_text_changed(self, text):
        if len(text) > 1 and text[0] == "0":
            self.setText(text[1:])


class QQ9SearchLineEdit(_QQ9LineEditBase):

    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent=parent)
        self.set_icon()
        self.setPlaceholderText("搜索")
        self.setFixedHeight(30)

    def set_icon(self):
        search_icon = QIcon(IMG_ROOT_DIR + "/find.png")
        self.addAction(search_icon, QLineEdit.LeadingPosition)